package net.onecloud.onecloud.account.resume;

import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import net.onecloud.onecloud.R;
import net.onecloud.onecloud.adapter.ResumeAdapter;
import net.onecloud.onecloud.application.App;
import net.onecloud.onecloud.base.BaseAppCompatActivity;
import net.onecloud.onecloud.model.ResultResume;
import net.onecloud.onecloud.model.ResultUser;
import net.onecloud.onecloud.network.NetWork;
import net.onecloud.onecloud.utils.LogUtils;
import net.onecloud.onecloud.utils.PreferencesUtils;
import net.onecloud.onecloud.utils.RecycleViewDivider;

import java.util.List;

import rx.Observable;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class ResumeActivity extends BaseAppCompatActivity {


    private RecyclerView mRecyclerView;
    private ResumeAdapter mResumeAdapter;
    private RecyclerView.LayoutManager mLayoutManager;
    Observer<ResultResume> queryResumeObserver = new Observer<ResultResume>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {
            LogUtils.e(e);
        }

        @Override
        public void onNext(ResultResume resultResume) {
            if (resultResume.getSuccess() == 1) {
                mResumeAdapter.setNewData(resultResume.getData());
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resume);
        setToolBarTitle("我的简历");
        HideLeftButton(false);
        HideRightButton(false);
        initViews();
        initListener();
        initData();
    }

    private void initData() {


        Observable.just(App.getUser())
                .observeOn(AndroidSchedulers.mainThread())
                .flatMap(new Func1<ResultUser.DataEntity, Observable<ResultResume>>() {
                    @Override
                    public Observable<ResultResume> call(ResultUser.DataEntity dataEntity) {
                        if (dataEntity != null) {
                            return NetWork.getResumeApi()
                                    .query1(dataEntity.getId())
                                    .subscribeOn(Schedulers.io());
                        }
                        showHit("请先登录");
                        return null;
                    }
                }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(queryResumeObserver);
    }

    private void initListener() {
    }

    private void initViews() {
        mRecyclerView = (RecyclerView) findViewById(R.id.m_recyclerview);
        mLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL));
        mResumeAdapter = new ResumeAdapter();
        mRecyclerView.setAdapter(mResumeAdapter);

    }

    @Override
    public void onClickRightButton() {
        Intent intent = new Intent(ResumeActivity.this, AddResumePage.class);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0) {
            if (resultCode == RESULT_OK) {
                ResultResume.DataEntity entity = data.getExtras().getParcelable("resume");
                if (entity != null)
                    mResumeAdapter.add(0, entity);
                    mRecyclerView.scrollToPosition(0);
            }
        }
    }


}
